<template>
  <el-dialog :title="title" :visible.sync="showDetail" width="700px" append-to-body class="my-dialog" @close="cancel">
    <el-form ref="form" :model="form" :rules="rules" label-width="90px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="所属组织" prop="isEmpty">
            <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择所属组织"
                        size="small"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="姓名" prop="nickName">
            <el-input v-model="form.nickName" placeholder="请输入联系电话"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择身份类型">
              <el-option
                v-for="item in dict.type.sys_work_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="phonenumber">
            <el-input v-model="form.phonenumber" placeholder="请输入联系电话"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" prop="sex">
            <el-select v-model="form.sex">
              <el-option
                v-for="item in dict.type.sys_user_sex"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="头像" prop="avatar">
            <image-upload v-model="form.avatar" :limit="1" :isShowTip="false"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证号" prop="idNumber">
            <el-input v-model="form.idNumber" placeholder="请输入身份证"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="籍贯住址" prop="address">
            <el-input v-model="form.address" placeholder="请输入籍贯住址"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="签发机关" prop="policeStation">
            <el-input v-model="form.policeStation" placeholder="请输入输签发机关"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="有效期" prop="period">
            <el-input v-model="form.period" placeholder="请输入有效期"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证（人脸面）" prop="idFrontPic">
            <image-upload v-model="form.idFrontPic" :limit="1" :isShowTip="false"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证（国徽面）" prop="idBackPic">
            <image-upload v-model="form.idBackPic" :limit="1" :isShowTip="false"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" size="small" plain>确 定</el-button>
      <el-button @click="cancel" size="small">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getUser, addUser, updateUser, deptTreeSelect} from '@/api/system/user'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listRole} from "@/api/system/role";

export default {
  props: ['visible', 'paramId'],
  name: "detail",
  dicts: ['sys_user_sex', 'sys_work_type'],
  components: {Treeselect},
  data() {
    return {
      // 弹出层标题
      title: "",
      showDetail: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickName: [
          {required: true, message: "姓名不能为空", trigger: "blur"}
        ],
        sex: [
          {required: true, message: "性别不能为空", trigger: "blur"}
        ],
        phonenumber: [
          {required: true, message: "联系电话不能为空", trigger: "blur"}
        ],
        idNumber: [
          {required: true, message: "身份证不能为空", trigger: "blur"}
        ],
        isEmpty: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        type: [
          {required: true, message: "身份类型不能为空", trigger: "blur"}
        ]
      },
      // 部门树选项
      deptOptions: [],
      roleList: null
    }
  },
  watch: {
    visible() {
      this.showDetail = this.visible
      this.reset();
      if (this.visible) {
        this.getDeptTree()
        if (this.paramId) {
          this.title = '保安信息详情'
          this.handleEditUpdate()
        } else {
          this.title = '新增保安信息'
        }
      }
    }
  },
  methods: {
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let data_ = this.$deepCopy(this.form)
          data_.userName = data_.phonenumber
          data_.roleId = 101
          data_.roleIds = [data_.roleId]
          delete data_.isEmpty
          if(!data_.deptId){
            this.$modal.msgError('所属组织不能为空');
            return
          }
          if (data_.userId != undefined) {
            updateUser(data_).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$emit('getInit')
              this.cancel()
            });
          } else {
            data_.password = '123456'
            data_.rawPassword = '123456'
            addUser(data_).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$emit('getInit')
              this.cancel()
            });
          }
        }
      });
    },
    /** 查询角色列表 */
    // getRoleList() {
    //   listRole({
    //     pageNum: 1,
    //     pageSize: 10
    //   }).then(response => {
    //       for (let item of response.rows) {
    //         if (item.roleName === '保安') {
    //           this.roleList = this.$deepCopy(item)
    //         }
    //       }
    //     }
    //   );
    // },
    handleEditUpdate(row) {
      this.reset();
      getUser(this.paramId).then(response => {
        delete response.data.dept
        response.data.type = response.data.type.toString()
        this.form = response.data;
        this.form.isEmpty = 1;
      });
    },
    // 取消按钮
    cancel() {
      this.$emit('update:visible', false)
      this.reset();
    },
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        deptId: undefined, //组织id
        userName: '', //用户名/账号
        nickName: '', // 姓名
        type: '', //类型
        phonenumber: '', //电话
        sex: '0', //性别
        avatar: '', //头像
        idNumber: '', //身份证
        status: 0, //帐号状态（0正常 1停用）
        isSecurity: 1, //是否保安
        address: '', // 籍贯
        idFrontPic: '',// 身份证正面
        idBackPic: '',// 身份证反面
        policeStation: '', // 签发派出所
        period: '', // 有效期
        facePic: '', // 面部打卡照片
        isEmpty: 1
      };
      this.cascaderData = []
      this.deptOptions = undefined
      this.resetForm("form");
    },
  }
}
</script>

<style scoped>

</style>
